package com.ktjy.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ktjy.pojo.SysRight;
import com.ktjy.pojo.SysRole;
import com.ktjy.pojo.SysRoleRight;
import com.ktjy.service.SysRightService;
import com.ktjy.service.SysRoleRightService;
import com.ktjy.service.SysRoleService;
import com.ktjy.service.SysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * 角色管理
 */

@Controller
public class RoleController {
    @Autowired
    SysRoleService roleService;
    @Autowired
    SysRightService sysRightService;
    @Autowired
    SysRoleRightService sysRoleRightService;
    @Autowired
    SysUserService sysUserService;

    @RequestMapping("/role/list")
    public Object getRoleList(SysRole sysRole, Model model, @RequestParam(defaultValue = "1") Integer pageIndex) {
        IPage<SysRole> page = new Page<>(pageIndex, 3);
        QueryWrapper<SysRole> queryWrapper = new QueryWrapper<>();
        model.addAttribute("roleName", sysRole.getRoleName());
        queryWrapper.eq(StringUtils.hasText(sysRole.getRoleName()), "role_name", sysRole.getRoleName());
        model.addAttribute("rolePage", roleService.page(page,queryWrapper));
        return "role/list";

    }

    @RequestMapping("/role/edit")
    public Object edit(Long roleId, Model model) {
        //获取用户拥有的权限
        List<SysRoleRight> rightList = sysUserService.getListSysRight(roleId);
        Set<String> roleRights = rightList.stream().map(sysRoleRight -> sysRoleRight.getRfRightCode()).collect(Collectors.toSet());
        model.addAttribute("role", roleService.getById(roleId));
        model.addAttribute("rights", sysRightService.list());
        model.addAttribute("roleRights", roleRights);
        return "/role/edit";
    }

    @RequestMapping("/role/add")
    public Object add(Model model) {
        model.addAttribute("rights", sysRightService.list());
        return "/role/add";
    }

    @RequestMapping("/role/save")
    @Transactional
    public Object saveOrUpdate(SysRole sysRole, String[] rightCodes) {
        System.out.println(sysRole.getRoleId());
        roleService.saveOrUpdate(sysRole);
        if (sysRole.getRoleId() <= 0) {
            roleService.addSysRole(sysRole);
            List<SysRoleRight> list = new ArrayList<>();
            for (String rightCode : rightCodes) {
                SysRoleRight sysRight = new SysRoleRight();
                sysRight.setRfRightCode(rightCode);
                sysRight.setRfRoleId(sysRole.getRoleId());
                list.add(sysRight);
            }
            sysRoleRightService.addSysRoleRight(list);
            return "redirect:/role/list";
        } else if (sysRole.getRoleId() > 0) {
            return "redirect:/role/edit?roleId="+sysRole.getRoleId();
        } else {
            return "redirect:/role/list";
        }
    }
}
